// Agent user in project COMP667-Proj

//Randomly generate a value, to be replace by some calculation of utility
price(_Service,X) :- .random(R) & X = (5*R)+10.

//initial belief that "game" is the initiator, to be dynamically loaded in the future
plays(gamerole,game).
plays(host,_hostname).
taken(accepted,false).
/* Plans */

// send a message to the initiator introducing myself as a participant
+plays(gamerole,In):true
	//set the variable Me with my name, i.e. host1
   <- .my_name(Me);
   	//introduce self to the gamerole as a host
    .send(In,tell,introduction(user,Me));
    .print("introducing myself:", Me, " to ",In).

   
//test CNP
+!testCNP(Ontology,Host):true
	//query and set the A variable to initiator
  <- ?plays(gamerole,A); 
  
  	//querry the proposal and set the CNPId,Task and Offer variables
	 ?proposal(CNPId,Task,Offer);
	 +plays(host,Host);
	 //print out the CNPId
	 .print("delivering on contract:",CNPId);
	//notifiy the initiator, testedCNP(_Ontology,_ContractId) exists in game.asl
     .send(A, tell, testedCNP(Ontology,CNPId)); //changed Ag to robot
     .print("notifying contestant of host");
     //assume the contestant has introduced themselves by now
     ?introduction(contestant,Contestant);
     .send(Contestant,tell,plays(host,Host)).

     
     // answer to Call For Proposal   
@c1 +cfp(CNPId,Task)[source(A)]
   :  plays(gamerole,A) & price(Task,Offer) & taken(accepted,false)
   <- +proposal(CNPId,Task,Offer); // remember my proposal
      .send(A,tell,user_propose(CNPId,Offer)).
	  
@c2 +cfp(CNPId,Task)[source(A)]//if the cfp didn't come from the initiator then
//refuse the cfp.
	: ~plays(gamerole,A)
	 <- .send(A,tell,user_refuse(CNPId)).

@c3 +cfp(CNPId,Task)[source(A)]:plays(gamerole,A) & taken(accepted,true)
<- .send(A,tell,user_refuse(CNPId)).


	 
@cr1 +accept_proposal(CNPId)
   :  proposal(CNPId,Task,Offer)
   <- .print("My proposal '",Offer,"' won CNP ",CNPId,
             " for ",Task,"!");
             -+taken(accepted,true);
			 !Task;
        	  ?plays(gamerole,A);
			 .send(A,tell,user_completed_contract(CNPId)).
      // do the task and report to initiator
      
@cr2 +reject_proposal(CNPId)
   <- .print("I lost CNP ",CNPId, ".");
      -proposal(CNPId,_,_,_). // clear memory
      
  
